﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;

//STANDARD for WPF App
//STANDARD for WPF App
//STANDARD for WPF App
//STANDARD for WPF App
//For : InkCanvas
//For : ExecutedRoutedEventArgs
//For : DrawingAttributes
//For : XamlWriter
//For : DrawingGroup
//STANDARD for WPF App
//For : BitmapImage etc etc
//STANDARD for WPF App
//For : Geometric shapes like Line

namespace BitsOfStuff.MyActions
{
    /// <summary>
    /// Main action , action that represents one CHAR
    /// </summary>
    [Serializable]
    public class MyShowAction : MyAction, IEquatable<MyShowAction>
    {
        private const int Priority = 1;
        private readonly char _char;
        private bool _autoritative;
        [NonSerialized]
        private InkCanvas _inkCanvas;

        /// <summary>
        /// Constructor of Show action
        /// </summary>
        /// <param name="c">Char that is stored in this action</param>
        /// <param name="inkCanvas">local resouces of this inkCanvas</param>
        public MyShowAction(char c, InkCanvas inkCanvas)
            : base(Priority)
        {
            _char = c;
            _inkCanvas = inkCanvas;
        }

        /// <summary>
        /// Construstor whitout params, from parent
        /// </summary>
        public MyShowAction()
            : base(Priority)
        {
        }

        /// <summary>
        /// Function for alocate local resources of InkCanvas
        /// </summary>
        /// <param name="inkCanvas">local object of inkCanvas</param>
        public override void assignInkCanvas(InkCanvas inkCanvas)
        {
            _inkCanvas = inkCanvas;
        }

        /// <summary>
        /// Function for geting , if action is doing something
        /// </summary>
        /// <returns>Status of action</returns>
        public override bool isDoingSomething()
        {
            return true;
        }

        /// <summary>
        /// Legasy action for making action of char , now we use Events
        /// </summary>
        /// <returns>status , if action was made</returns>
        public override bool makeAction()
        {
            return true;
        }

        /// <summary>
        /// Funstino for geting stored char
        /// </summary>
        /// <returns>Stored char</returns>
        public char getChar()
        {
            return _char;
        }

        /*
                private void showTextBox(char c)
                {
                    if (_inkCanvas.Dispatcher.CheckAccess())
                    {
                        showTextBoxSafe(c);
                    }
                    else
                    {
                        _inkCanvas.Dispatcher.Invoke(new Action(() => showTextBoxSafe(c)));
                    }
                }
        */

        /*
                [STAThread]
                private RichTextBox makeTextBox()
                {
                    var myTextBox = new RichTextBox { Height = 200, Width = 200, FontSize = 150, FontFamily = new FontFamily("Segoe") };

                    var color = new Color { A = 153, R = 59, G = 55, B = 55 };
                    myTextBox.Foreground = new SolidColorBrush(color);
                    Brush backgraund = new DrawingBrush();
                    backgraund.Opacity = 0;
                    myTextBox.BorderBrush = backgraund;
                    myTextBox.Background = backgraund;

                    return myTextBox;
                }
        */

        /*
                [STAThread]
                private void showTextBoxSafe(char c)
                {
                    var myTextBox = makeTextBox();
                    myTextBox.AppendText(c.ToString(CultureInfo.InvariantCulture));
                    SupportDrawnFunctions.reDrawnInkCanvas(_inkCanvas);
                    //Can acees on curent thread
                    InkCanvas.SetLeft(myTextBox, 300);
                    InkCanvas.SetTop(myTextBox, 50);
                    _inkCanvas.Children.Add(myTextBox);
                }
        */

        /// <summary>
        /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
        /// </summary>
        /// <returns>
        /// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes <paramref name="obj"/> in the sort order. Zero This instance occurs in the same position in the sort order as <paramref name="obj"/>. Greater than zero This instance follows <paramref name="obj"/> in the sort order.
        /// </returns>
        /// <param name="obj">An object to compare with this instance. </param><exception cref="T:System.ArgumentException"><paramref name="obj"/> is not the same type as this instance. </exception><filterpriority>2</filterpriority>
        public override int CompareTo(object obj)
        {
            return 1;
        }

        /// <summary>
        /// Function for comparing this actions to List of actions
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override int CompareTo(List<MyAction> obj)
        {
            if (obj != null)
            {
                var myShowAction = obj[0] as MyShowAction;
                if (myShowAction != null)
                {
                    if ((myShowAction).getChar() == _char)
                    {
                        if ((myShowAction).getChar().CompareTo(_char) == 0)
                            return 0; // If contains same char to show
                        return -1;// If both is ShowAction , but NOT contain same char
                    }
                }
            }
            return 1;
        }

        /// <summary>
        /// Function for comparing two actions
        /// </summary>
        /// <param name="obj">Action for comparing</param>
        /// <returns>result of comparing</returns>
        public override int CompareTo(MyAction obj)
        {
            var myShowAction = obj as MyShowAction;
            if (myShowAction != null)
            {
                return _char.CompareTo((myShowAction).getChar());
            }
            return 1;
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public override bool Equals(MyAction other)
        {
            var myShowAction = other as MyShowAction;
            if (myShowAction != null)
            {
                return _char == (myShowAction).getChar();
            }
            return false;
        }
    }

    /// <summary>
    /// Function for comparing showActions
    /// </summary>
    public class MyShowActionComparer : IEqualityComparer<MyAction>
    {
        public bool Equals(MyAction x, MyAction y)
        {
            return x.Equals(y);
        }

        public int GetHashCode(MyAction obj)
        {
            if (obj is MyShowAction)
                return ((MyShowAction)obj).getChar();
            else
            {
                return obj.getPriority();
            }
        }
    }
}